extends Area2D

#设置单个方块元素的属性如2碰撞区域，材质和位置
class_name BlockUnit
@onready var sprite_2d = $Sprite2D
@onready var collision_shape_2d = $CollisionShape2D
signal block_unit_is_dragging
var is_dragging
var drag_offset = Vector2.ZERO
var target_position = Vector2.ZERO
var start_position = Vector2.ZERO
var should_lock = 0
@onready var timer = $Timer
var bounds = {
	"min_x":107,
	"max_x":142,
	"max_y":912,
}  # 这个是整个布局的边界再加上半个block的宽度
func set_texture(texture:Texture2D):
	sprite_2d.texture = texture

func get_size():
	return collision_shape_2d.shape.get_rect().size

	
func _ready():
	# 连接输入事件
	print("_ready postion"+str(global_position))
	#target_position.x = global_position.x / 2
	#target_position.y = global_position.y / 2
	#print("_ready target_position"+str(target_position))
	input_event.connect(_on_input_event)

func _on_input_event(_viewport, event, _shape_idx):
	if event is InputEventMouseButton and event.button_index == MOUSE_BUTTON_LEFT:
		if event.pressed:
			timer.stop()
			#block_unit_is_dragging.emit(true)
			# 鼠标按下时开始拖拽
			is_dragging = true
			
			drag_offset = position - get_global_mouse_position()
		else:
			#block_unit_is_dragging.emit(false)
			is_dragging = false
			# 鼠标松开时设置目标位置
			target_position = get_global_mouse_position() + drag_offset
			timer.start()
func _physics_process(_delta):
	if is_dragging:
		# 更新位置跟随鼠标
		position = get_global_mouse_position() + drag_offset
		print("dragging position.x:"+str(position))
	elif position != target_position:

		# 移动到目标位置
		#target_position.y = -370+49
		position = target_position
		print("stop dragging position.x:"+str(position))
		#if should_lock:
			#timer.stop()
		


func move(direction:Vector2):
	var new_position = calculate_global_position(direction,position)
	print(new_position)
	if new_position:
		target_position = new_position
		#if direction != Vector2.DOWN:
			#hard_drop_ghost.call_deferred()
		return true
	return false

func calculate_global_position(direction:Vector2,starting_global_position:Vector2):

	##check for collision with other tetromino
	#
	#if is_colliding_with_other_tetromino(direction,starting_global_position):
		#return null
	#
	##check for collision with game bounds
	if !is_within_game_bounds(direction,starting_global_position):
		print("with game bounds")
		return null
	#print(collision_shape_2d.shape.get_rect().size.x)
	return starting_global_position+direction*collision_shape_2d.shape.get_rect().size.x

func is_colliding_with_other_tetromino(direction:Vector2,starting_global_position:Vector2):
	pass
	#for tetromino in other_tetrominos:
		#var tetromino_pieces = tetromino.get_children().filter(func (c):return c is Piece)
		#for tetromino_piece in tetromino_pieces:
			#for piece in pieces:
				#if starting_global_position + piece.position + direction*piece.get_size().x == tetromino.global_position + tetromino_piece.position:
					#return true
	#return false

func is_within_game_bounds(direction:Vector2,starting_global_position:Vector2):
	#pass
	var new_position = starting_global_position+direction*collision_shape_2d.shape.get_rect().size.x
	print("new_position = "+str(new_position))
	#if new_position.x < bounds.get("min_x") || new_position.x > bounds.get("max_x") || new_position.y > bounds.get("max_y"):
		#print("collid with bounds.....")
		#target_position.y = bounds.get("max_y")
		#print(target_position.y)
		#should_lock = 1
		#return false
	return true	

func _on_block_unit_is_dragging() -> void:
	print("blockunit _on_block_unit_is_dragging")
	pass # Replace with function body.


func _on_timer_timeout() -> void:
	#pass
	move(Vector2.DOWN)
	#pass # Replace with function body.
